﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ykglxt
{
    public partial class Form_YPSXSZ : Form
    {
        public Form_YPSXSZ()
        {
            InitializeComponent();
        }

        bool empty = true;
        int firsttime = 0;
        DataSet ds = new DataSet();
        SqlConnection con;
        SqlDataAdapter sdaYPSX;
        SqlDataAdapter sdaYPCRXX;
        SqlCommandBuilder scbYPSX;
        SqlCommandBuilder scbYPCRXX;
        DataRow drYPSX;
        DataRow drYPCRXX;

        private void bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e)
        {
            int rowindx = (dgvYPSXSZ.SelectedRows[0].Index - 1 + dgvYPSXSZ.RowCount) % dgvYPSXSZ.RowCount;
            dgvYPSXSZ.ClearSelection();
            dgvYPSXSZ.Rows[rowindx].Selected = true;
            dgvYPSXSZ.FirstDisplayedScrollingRowIndex = dgvYPSXSZ.SelectedRows[0].Index;
        }

        private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e)
        {
            int rowIndex = (dgvYPSXSZ.SelectedRows[0].Index + 1) % dgvYPSXSZ.RowCount;
            dgvYPSXSZ.ClearSelection();
            dgvYPSXSZ.Rows[rowIndex].Selected = true;
            dgvYPSXSZ.FirstDisplayedScrollingRowIndex = rowIndex;
            if (rowIndex < dgvYPSXSZ.FirstDisplayedScrollingRowIndex)
            {
                dgvYPSXSZ.FirstDisplayedScrollingRowIndex = rowIndex;
            }
            else if (!dgvYPSXSZ.Rows[rowIndex].Displayed)
            {
                dgvYPSXSZ.FirstDisplayedScrollingRowIndex++;
            }

        }

        private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)
        {
            dgvYPSXSZ.ClearSelection();
            dgvYPSXSZ.Rows[0].Selected = true;
            dgvYPSXSZ.FirstDisplayedScrollingRowIndex = 0;
        }

        private void bindingNavigatorMoveLastItem_Click(object sender, EventArgs e)
        {
            dgvYPSXSZ.ClearSelection();
            dgvYPSXSZ.Rows[dgvYPSXSZ.RowCount - 2].Selected = true;
            dgvYPSXSZ.FirstDisplayedScrollingRowIndex = dgvYPSXSZ.RowCount - 1;

        }

        private void Form_YPSXSZ_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“yKGLXTDataSet.Dic_PZWH”中。您可以根据需要移动或移除它。
            this.dic_PZWHTableAdapter.Fill(this.yKGLXTDataSet.Dic_PZWH);
            // TODO: 这行代码将数据加载到表“yKGLXTDataSet.Dic_SCS”中。您可以根据需要移动或移除它。
            this.dic_SCSTableAdapter.Fill(this.yKGLXTDataSet.Dic_SCS);
            // TODO: 这行代码将数据加载到表“yKGLXTDataSet.Dic_GYS”中。您可以根据需要移动或移除它。
            this.dic_GYSTableAdapter.Fill(this.yKGLXTDataSet.Dic_GYS);
            // TODO: 这行代码将数据加载到表“yKGLXTDataSet.Dic_HW”中。您可以根据需要移动或移除它。
            this.dic_HWTableAdapter.Fill(this.yKGLXTDataSet.Dic_HW);
            txbYPDW.Focus();
            con = new SqlConnection(PublicUnit.ConnetionString);
            try
            {
                using(SqlConnection contmp = new SqlConnection(PublicUnit.ConnetionString))
                {
                    SqlCommand com = new SqlCommand("INSERT INTO ML_YPSX ( YKBM, YPBM, DJDX, DJGX, KCDX, KCGX, HWBM, YJYXQ) SELECT '"+
                        PublicUnit.UserKSBM + "', YP.BM, 0, 0, 0, 0, '', 0 FROM Dic_YP YP WHERE YP.BM NOT IN (SELECT YPBM FROM ML_YPSX WHERE YKBM = '"+
                        PublicUnit.UserKSBM + "') AND YP.LX IN (0,4)",contmp);
                    contmp.Open();
                    com.ExecuteNonQuery();
                    SqlDataAdapter sda = new SqlDataAdapter("SELECT SX.YPBM, YP.ZJF, YP.MC, YP.JLDW, YP.BZDW from ML_YPSX SX, Dic_YP YP WHERE SX.YPBM = YP.BM AND SX.YKBM =  '" +PublicUnit.UserKSBM +"'",con);
                    sda.Fill(ds,"YP");
                    dgvYPSXSZ.DataSource = ds.Tables["YP"];
                    dgvYPSXSZ.ClearSelection();

                    sdaYPSX = new SqlDataAdapter("SELECT * FROM ML_YPSX WHERE YKBM = '" + PublicUnit.UserKSBM + "'", con);
                    sdaYPCRXX = new SqlDataAdapter("SELECT * FROM ML_CRXX WHERE CR=0 AND YKBM = '" + PublicUnit.UserKSBM + "'", con);
                    scbYPSX = new SqlCommandBuilder(sdaYPSX);
                    scbYPCRXX = new SqlCommandBuilder(sdaYPCRXX);
                    sdaYPSX.Fill(ds, "YPSX");
                    drYPSX = ds.Tables["YPSX"].NewRow();
                    ds.Tables["YPSX"].PrimaryKey = new DataColumn[] { ds.Tables["YPSX"].Columns[1] };
                    sdaYPCRXX.Fill(ds, "YPCRXX");
                    drYPCRXX = ds.Tables["YPCRXX"].NewRow();
                    ds.Tables["YPCRXX"].PrimaryKey = new DataColumn[] { ds.Tables["YPCRXX"].Columns[14] };
                }
            }
            catch(SqlException ex)
            {
                if (ex.Number == 18456)
                    MessageBox.Show("网络故障，请稍后再试！", "警告" + ex.Number, MessageBoxButtons.OK, MessageBoxIcon.Warning);   
            }

        }

        private void txbYPDW_KeyDown(object sender, KeyEventArgs e)
        {
            if (empty)
                return;

            if (e.KeyCode == Keys.Down)
            {
                dgvYPDW.Focus();
                if (dgvYPDW.CurrentRow.Index + 1 < dgvYPDW.Rows.Count)
                {
                    dgvYPDW.CurrentCell = dgvYPDW[0, dgvYPDW.CurrentRow.Index + 1];
                }
            }

            if (e.KeyCode == Keys.Enter)
            {
                this.txbYPDW.Text = this.dgvYPDW.CurrentRow.Cells[0].Value.ToString();
                this.dgvYPDW.Visible = false;
            }
        }

        private void txbYPDW_TextChanged(object sender, EventArgs e)
        {
            try
            {
                this.dgvYPDW.Top = this.txbYPDW.Top + this.txbYPDW.Height;

                if (firsttime == 0)
                {
                    firsttime = 1;
                    empty = getData();
                }

                if (empty)
                    return;

                if (txbYPDW.Text == " ")
                {
                    if (ds.Tables["YPDW"].DefaultView.Count != 0)
                        dgvYPDW.Visible = true;

                }
                else
                {
                    string dh = "编码 like '" + txbYPDW.Text.Trim() + "%' or 药品名称 like '%" + txbYPDW.Text.Trim() + "%'"+ " or 助记码 like '" + txbYPDW.Text.Trim() + "%'" ;
                    ds.Tables["YPDW"].DefaultView.RowFilter = dh.ToString();
                }
                if (ds.Tables["YPDW"].DefaultView.Count != 0)
                {
                    dgvYPDW.Visible = true;
                    dgvYPDW.CurrentCell = dgvYPDW[0, 0];
                }

                if (ds.Tables[0].DefaultView.Count == 0)//找不到，模糊查询
                {
                    string dh = "编码 like '%" + txbYPDW.Text.Trim() + "%'  ";
                    ds.Tables["YPDW"].DefaultView.RowFilter = dh.ToString();
                    if (ds.Tables["YPDW"].DefaultView.Count != 0)
                    {
                        dgvYPDW.Visible = true;
                        dgvYPDW.CurrentCell = dgvYPDW[0, 0];
                    }

                    if (ds.Tables["YPDW"].DefaultView.Count == 0)
                    {
                        dgvYPDW.Visible = false;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private bool getData()
        {
            bool res;
                try
                {
                    res = false;

                    using (SqlConnection conn = new SqlConnection(PublicUnit.ConnetionString))
                    {
                        try
                        {
                            conn.Open();
                            SqlDataAdapter da = new SqlDataAdapter("select bm as '编码',zjf as '助记码',mc AS '药品名称',gg AS '规格',dj as '单价',sccs AS '生产产家' from dic_yp where lx in (0,4)", conn);
                            da.Fill(ds,"YPDW");
                            this.dgvYPDW.DataSource = ds.Tables["YPDW"].DefaultView;
                            res = false;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("error!" + ex.ToString());
                            res = true;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("error!" + ex.ToString());
                    res = true;
                }
            return res;
        }

        private void txbYPDW_KeyPress(object sender, KeyPressEventArgs e)
        {
             if (txbYPDW.ReadOnly) // 只读, 不处理 
                return;

            if ((int)e.KeyChar <= 32) // 特殊键(含空格), 不处理 
                return;

            if (!char.IsDigit(e.KeyChar) && !char.IsLetter(e.KeyChar)) // 非数字字母键, 放弃该输入 
            {
                e.Handled = true;
                return;
            }
        }

        private void dgvYPDW_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            this.txbYPDW.Text = this.dgvYPDW.Rows[dgvYPDW.CurrentCell.RowIndex].Cells[0].Value.ToString();
            this.dgvYPDW.Visible = false;
            for (int i = 0; i < this.dgvYPSXSZ.Rows.Count; i++)
            {
                if (txbYPDW.Text == this.dgvYPSXSZ.Rows[i].Cells[0].Value.ToString())
                {
                    this.dgvYPSXSZ.ClearSelection();
                    this.dgvYPSXSZ.Rows[i].Selected = true;
                    this.dgvYPSXSZ.CurrentCell = this.dgvYPSXSZ.Rows[i].Cells[0];
                    return;
                }
            }
        }

        private void dgvYPDW_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txbYPDW.Text = this.dgvYPDW.Rows[dgvYPDW.CurrentCell.RowIndex].Cells[0].Value.ToString();
                this.dgvYPDW.Visible = false;
                for (int i = 0; i < this.dgvYPSXSZ.Rows.Count; i++)
                {
                    if (txbYPDW.Text == this.dgvYPSXSZ[this.dgvYPSXSZ.Columns[0].Index, this.dgvYPSXSZ.Rows[i].Index].Value.ToString())
                    {
                        this.dgvYPSXSZ.ClearSelection();
                        this.dgvYPSXSZ.Rows[i].Selected = true;
                        this.dgvYPSXSZ.CurrentCell = this.dgvYPSXSZ.Rows[i].Cells[0];
                        return;
                    }
                }
        }


        }

        private void dgvYPSXSZ_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                drYPSX = ds.Tables["YPSX"].Rows.Find(dgvYPSXSZ.Rows[dgvYPSXSZ.CurrentCell.RowIndex].Cells[0].Value.ToString().Trim());
                tbDJDX.Text = drYPSX[2].ToString().Trim();
                tbDJGX.Text = drYPSX[3].ToString().Trim();
                tbKCDX.Text = drYPSX[4].ToString().Trim();
                tbKCGX.Text = drYPSX[5].ToString().Trim();
                cbxHW.SelectedValue = drYPSX[6].ToString().Trim();
                tbTSYXQ.Text = drYPSX[7].ToString().Trim();
                drYPCRXX = ds.Tables["YPCRXX"].Rows.Find(dgvYPSXSZ.Rows[dgvYPSXSZ.CurrentCell.RowIndex].Cells[0].Value.ToString().Trim());
                if (drYPCRXX != null)
                {
                    cbxGYS.SelectedValue = drYPCRXX[4].ToString().Trim();
                    cbxSCS.SelectedValue = drYPCRXX[5].ToString().Trim();
                    tbxJJ.Text = drYPCRXX[7].ToString().Trim();
                    tbxLSJ.Text = drYPCRXX[9].ToString().Trim();
                    cbxPJWH.SelectedValue = drYPCRXX[10].ToString().Trim();
                    tbxPH.Text = drYPCRXX[15].ToString().Trim();
                    dtpYXQ.Text = drYPCRXX[12].ToString().Trim();
                }
            }
            catch (SqlException ex)
            {
                if (ex.Number == 18456)
                    MessageBox.Show("网络故障，请稍后再试！", "警告" + ex.Number, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void tsbSave_Click(object sender, EventArgs e)
        {
            try
            {
                sdaYPSX.Update(ds.Tables["YPSX"]).ToString();
                sdaYPCRXX.Update(ds.Tables["YPCRXX"]);
                MessageBox.Show("保存成功！");
            }
            catch (SqlException ex)
            {
                if (ex.Number == 18456)
                    MessageBox.Show("网络故障，请稍后再试！", "警告" + ex.Number, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            refresh();
        }

        void refresh()
        {
            try
            {
                using (SqlConnection contmp = new SqlConnection(PublicUnit.ConnetionString))
                {
                    SqlDataAdapter sda = new SqlDataAdapter("SELECT SX.YPBM, YP.ZJF, YP.MC, YP.JLDW, YP.BZDW from ML_YPSX SX, Dic_YP YP WHERE SX.YPBM = YP.BM AND SX.YKBM =  '" + PublicUnit.UserKSBM + "'", con);
                    sda.Fill(ds, "YP");
                    dgvYPSXSZ.DataSource = ds.Tables["YP"];
                }
            }
            catch (SqlException ex)
            {
                if (ex.Number == 18456)
                    MessageBox.Show("网络故障，请稍后再试！", "警告" + ex.Number, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            dgvYPSXSZ.ClearSelection();
        }

        private void tbDJDX_TextChanged(object sender, EventArgs e)
        {
            if (tbDJDX.Focused)
                drYPSX[2] = tbDJDX.Text.Trim();
        }

        private void tbDJGX_TextChanged(object sender, EventArgs e)
        {
            if (tbDJGX.Focused)
                drYPSX[3] = tbDJGX.Text.Trim();
        }

        private void tbKCDX_TextChanged(object sender, EventArgs e)
        {
            if (tbKCDX.Focused)
                drYPSX[4] = tbKCDX.Text.Trim();
        }

        private void tbKCGX_TextChanged(object sender, EventArgs e)
        {
            if (tbKCGX.Focused)
                drYPSX[5] = tbKCGX.Text.Trim();
        }

        private void cbxHW_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxHW.Focused)
                drYPSX[6] = cbxHW.SelectedValue.ToString().Trim();
        }

        private void tbTSYXQ_TextChanged(object sender, EventArgs e)
        {
            if (tbTSYXQ.Focused)
                drYPSX[7] = tbTSYXQ.Text.Trim();
        }

        private void cbxGYS_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drYPCRXX != null && cbxGYS.Focused)
                drYPCRXX[4] = cbxGYS.SelectedValue.ToString().Trim();
        }

        private void cbxSCS_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drYPCRXX != null && cbxSCS.Focused)
                drYPCRXX[5] = cbxSCS.SelectedValue.ToString().Trim();
        }

        private void tbxJJ_TextChanged(object sender, EventArgs e)
        {
            if (drYPCRXX != null && tbxJJ.Focused)
                drYPCRXX[7] = tbxJJ.Text.Trim();
        }

        private void tbxLSJ_TextChanged(object sender, EventArgs e)
        {
            if (drYPCRXX != null && tbxLSJ.Focused)
                drYPCRXX[9] = tbxLSJ.Text.Trim() ;
        }

        private void cbxPJWH_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drYPCRXX != null && cbxPJWH.Focused)
                drYPCRXX[10] = cbxPJWH.SelectedValue.ToString().Trim();
        }

        private void tbxPH_TextChanged(object sender, EventArgs e)
        {
            if (drYPCRXX != null && tbxPH.Focused)
                drYPCRXX[15] = tbxPH.Text.Trim();
        }

        private void dtpYXQ_ValueChanged(object sender, EventArgs e)
        {
            if (drYPCRXX != null && dtpYXQ.Focused)
                drYPCRXX[12] = dtpYXQ.Text.Trim();
        }
    }
}
